//
//  XWMainTabBar.swift
//  XWSinaWeibo
//
//  Created by 徐小伟 on 16/7/20.
//  Copyright © 2016年 Xuxiaowei. All rights reserved.
//

import UIKit

class XWMainTabBar: UITabBar {

   
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(plusBtn)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        遍历子视图
        let itemW=self.frame.width/5
        let itemH=self.frame.height
        let itemRect=CGRect(x: 0, y: 0, width: itemW, height: itemH)
        
        var index:CGFloat=0
        
        for subView in subviews{
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                subView.frame=CGRectOffset(itemRect, index*itemW, 0)
                if index==1{
                    index++
                }
                
                index++
            }
        }
        
        plusBtn.frame=CGRectOffset(itemRect, 2*itemW, 0)
    }
    
//    懒加载加号按钮
    lazy var plusBtn:UIButton={
        let btn=UIButton()
        
//        设置加号按钮图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Selected)
//        设置加号按钮的背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Selected)
        
        return btn
    }()

}
